package com.java.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.GuidanceInfo;
import com.java.entity.event.GuidanceInfoEvent;
import com.java.mapper.AccountInfoMapper;
import com.java.mapper.GuidanceInfoMapper;
import com.java.service.GuidanceService;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class GuidanceServiceImpl implements GuidanceService {

	@Autowired private GuidanceInfoMapper guidanceMapper;
	
	@Autowired private AccountInfoMapper accountMapper;
	
	@Override
	public BaseRespone sendGuidance(String sendId, String receiveId, String text) {
		GuidanceInfo info = new GuidanceInfo();
		info.setGuidanceId(MyTools.getUUID());
		info.setGuidanceSendId(sendId);
		info.setGuidanceReceiveId(receiveId);
		info.setGuidanceText(text);
		info.setGuidanceTime(new Date());
		int row = guidanceMapper.insertSelective(info);
		return BaseRespone.result(row, "已发送咨询");
	}

	@Override
	public BaseRespone replyGuidance(String guidanceId, String guidanceReply) {
		GuidanceInfo info = guidanceMapper.selectByKey(guidanceId);
		info.setGuidanceReply(guidanceReply).setGuidanceReplyTime(new Date());
		int row = guidanceMapper.updateSelective(info);
		return BaseRespone.result(row, "回复成功");
	}

	@Override
	public TableRespone getGuidances(int page, int size) {
		MyMap mm = new MyMap();
		List<GuidanceInfoEvent> list = guidanceMapper.selectAll(mm, page, size);
		for (GuidanceInfoEvent guidanceInfoEvent : list) {
			guidanceInfoEvent.setSend(accountMapper.selectByKey(guidanceInfoEvent.getGuidanceSendId()));
			guidanceInfoEvent.setReceive(accountMapper.selectByKey(guidanceInfoEvent.getGuidanceReceiveId()));
		}
		long ll = guidanceMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

	@Override
	public TableRespone getGuidancesBySend(String userId, int page, int size) {
		MyMap mm = new MyMap();
		mm.put("guidanceSendId", userId);
		List<GuidanceInfoEvent> list = guidanceMapper.selectAll(mm, page, size);
		for (GuidanceInfoEvent guidanceInfoEvent : list) {
			guidanceInfoEvent.setSend(accountMapper.selectByKey(guidanceInfoEvent.getGuidanceSendId()));
			guidanceInfoEvent.setReceive(accountMapper.selectByKey(guidanceInfoEvent.getGuidanceReceiveId()));
		}
		long ll = guidanceMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

	@Override
	public TableRespone getGuidancesByReceive(String userId, int page, int size) {
		MyMap mm = new MyMap();
		mm.put("guidanceReceiveId", userId);
		List<GuidanceInfoEvent> list = guidanceMapper.selectAll(mm, page, size);
		for (GuidanceInfoEvent guidanceInfoEvent : list) {
			guidanceInfoEvent.setSend(accountMapper.selectByKey(guidanceInfoEvent.getGuidanceSendId()));
			guidanceInfoEvent.setReceive(accountMapper.selectByKey(guidanceInfoEvent.getGuidanceReceiveId()));
		}
		long ll = guidanceMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

	@Override
	public BaseRespone delGuidance(String guidanceId) {
		int row = guidanceMapper.deleteByKey(guidanceId);
		return BaseRespone.result(row, "已删除");
	}

}
